﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Admin.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    OpenAuthorPopup
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%Html.RenderPartial("_PopupAuthor"); %>
    <script type="text/javascript" charset="utf-8">
        function SearchAuthorName() {
            removeAllInLeft();
            var authorName = $("#txtAuthorName").val();
            var All = $("#AllAuthorList option");
            var FinalLeft = $("#LeftAuthorList");
            var numberAll = parseInt(All.length);
            for (var i = 0; i < numberAll; i++) {
                var content = All[i].innerText;
                if (content == undefined) {
                    content = All[i].textContent
                }
                if (content.toLowerCase().indexOf(authorName.toLowerCase()) != -1) {
                    FinalLeft.append('<option value=' + All[i].getAttribute('value') + '>' + content + '</option>');
                }
            }
        }
        function removeAllInLeft() {
            var Left = $("#LeftAuthorList option");
            Left.remove();
        }
        function AddAuthor() {
            var authorName = $("#txtAuthorName").val();
            var href = "/Author/DoCreateAuthor";
            $.post(href, { authorName: authorName },
           function (data) {
               if (data != "0") {
                   AddAuthorSuccess(data, authorName);
               }
           });
       }
       function AddAuthorSuccess(Id, Name) {
           //sau khi add author thanh cong thì add thang zo trong leftdiv và hiddendiv
           var Left = $("#LeftAuthorList");
           Left.append('<option value=' + Id + '>' + Name + '</option>');
           var All = $("#AllAuthorList");
           All.append('<option value=' + Id + '>' + Name + '</option>');
       }
        
        $('#LeftAuthorList').dblclick(function () {
            LeftToRight();
        });

        function LeftToRight() {
            var Left = $("#LeftAuthorList option:selected");
            var Right = $("#RightAuthorList option");
            var FinalRight = $("#RightAuthorList");
            var numberSelected = parseInt(Left.length);
            var NumberRowInRight = parseInt(Right.length);
            for (var i = 0; i < numberSelected; i++) {
                var duplicate = 0;
                for (var j = 0; j < NumberRowInRight; j++) {
                    if (Right[j].getAttribute("value") == Left[i].getAttribute("value")) {
                        duplicate = 1;
                    }
                }
                if (duplicate == 0) {
                    var content = Left[i].innerText;
                    if (content == undefined) {
                        content = Left[i].textContent
                    }

                    FinalRight.append('<option value=' + Left[i].getAttribute('value') + '>' + content + '</option>');
                }
            }
        }
        function RemoveFromRight() {
            var Right = $("#RightAuthorList option:selected");
            var numberSelected = parseInt(Right.length);
            for (var i = 0; i < numberSelected; i++) {
                $("#RightAuthorList option[value=" + Right[i].getAttribute('value') + "]").replaceWith("");
            }
        }
        function SaveAuthor() {
            //step 1: get list author in Right Div
            var Author = "";
            var Right = $("#RightAuthorList option");
            var numberSelected = parseInt(Right.length);
            for (var i = 0; i < numberSelected; i++) {
                var content = Right[i].innerText;
                if (content == undefined) {
                    content = Right[i].textContent
                }
                Author += "," + Right[i].getAttribute('value');
                Author += "!" + content;
            }
            ////Step 2: Post data to AuthorController to write session, Close popup and reload ajax under div
            //            var href = "/Author/SessionAuthorSave";
            //            $.post(href, { AuthorIds: AuthorIds },
            //           function (data) {
            //               window.opener.loadDivAuthor();
            //               window.close;
            //           });
            // step 2: give data from popup to opener (in Book/Create)

            window.opener.loadDivAuthor(Author);
            window.close();


        }

    </script>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="Navigation" runat="server">
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="MenuLeft" runat="server">
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="Toolbar" runat="server">
</asp:Content>
